<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

$app->get('/', function () use ($app) {
    return $app->version();
});

// 版本比较
$app->get('Version/{devicetype}/{version}',function ($devicetype,$version){
    if ($currentVersion = app('db')->table('app_version')->where('APKTYPE',$devicetype)->orderBy('create_time', 'desc')->take(1)->value('VERSIONCODE')){ // 获取当前版本
      switch (version_compare($currentVersion,$version)){
        case -1: // 当前可用版本号<正在使用版本号
          $code = 1; // 不提示
          break;
        case 0: // 当前可用版本号=正在使用版本号
          $code = 1; // 不提示
        break;
        case 1: // 当前可用版本号>正在使用版本号
          $code = (int)app('db')->table('app_version')->where('APKTYPE',$devicetype)->value('updateType'); // 获取更新方式
      }
      return response()->json(['status_code'=>HTTP_SUCCESS,'code'=>$code,'content'=>app('db')->table('app_version')->where('APKTYPE',$devicetype)->value('CONTENT')]);
    } else {
      return response()->json(['status_code'=>SYSTEM_ERROR]);
    }
});

// 清除缓存
$app->get('DelCache/{id}',function($id){
    switch($id){
        case 'top':
            if(app('cache')->forget(md5('index_today_top_source'))){
                return response()->json(['status_code'=>HTTP_SUCCESS]);
            }else{
                return response()->json(['status_code'=>SYSTEM_ERROR]);
            }
        case 'report':
            if(app('cache')->forget(md5('is_index_report_source'))){
                return response()->json(['status_code'=>HTTP_SUCCESS]);
            }else{
                return response()->json(['status_code'=>SYSTEM_ERROR]);
            }
        case 'nav':
            $source_prefix = config('upload.bucket_userauth_domain');
            $image_thumbnail = env('APP_INDEX_NAV_THUMBNAIL','');
            $query = "select t.acskey,t.recotype,t.otherkey,concat('$source_prefix',bpicsite,'/',bpicname,'$image_thumbnail') as bpicurl,weburl,name,programtv,address,t.title,t.islogin from v_home_reco t where type = '1' and STATUS=1 order by ORDERBY asc ";
            $key = md5($query);
            if(app('cache')->forget($key)){
                return response()->json(['status_code'=>HTTP_SUCCESS]);
            }else{
                return response()->json(['status_code'=>SYSTEM_ERROR]);
            }
        case 'index_func_btn':
            $query = "select t.id,t.order_num from paike_home_func_btn t";
            $key = md5($query);
            if(app('cache')->forget($key)){
              return response()->json(['status_code'=>HTTP_SUCCESS]);
            }else{
              return response()->json(['status_code'=>SYSTEM_ERROR]);
            }
        default:
            if(app('cache')->forget(md5('index_model_type'.$id))){
                return response()->json(['status_code'=>HTTP_SUCCESS]);
            }else{
                return response()->json(['status_code'=>SYSTEM_ERROR]);
            }
    }
});

/**
 *七牛视频转码
 */
$app->get("VideoFops/{file_url}",function($file_url){
    $result = qiniu_fops(urldecode($file_url));
    $result = $result[0];
    if($result){
        return response()->json(['status_code'=>HTTP_SUCCESS]);
    }else{
        return response()->json(['status_code'=>SYSTEM_ERROR]);
    }
});

//线索和证据 html
$app->get('{type:clue|evidence}.html',function($type){
    return response(file_get_contents('./html/'.$type.'.html'),HTTP_SUCCESS,['Content-type:text/html;charset=utf-8']);
});
$app->get('oooo',function(){
    app('cache')->forget(md5('evidence_type_list'));
});

require_once "server.php";